package com.atguigu.jxc.service.impl;


import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    SupplierDao supplierDao;

    //查询供应商列表
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        Map<String,Object> map = new HashMap<>();

        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Supplier> Suppliers = supplierDao.getSupplierList(offSet, rows, supplierName);

        map.put("total", supplierDao.getSupplierCount(supplierName));

        map.put("rows", Suppliers);
        return map;
    }

    @Transactional
    //添加供应商
    @Override
    public void addSupplier(Supplier supplier,Integer supplierId) {

        if (supplierId != null){

            supplierDao.updateSupplierById(supplier);

        }else {

            supplierDao.addSupplier(supplier);

        }




    }

    @Override
    public void deleteSupplier(String ids) {

             String[] arrays =   ids.split(",");

            supplierDao.deleteSupplier(arrays);



    }
}
